import { reqLogin,reqLogout } from '@/api/user'

export default {
  namespaced: true,
  state: {
    name: localStorage.getItem('name'),
    token: localStorage.getItem('token')
  },
  actions: {
    async login({ commit }, { phone, password }) {
      const user = await reqLogin(phone, password)
      commit('LOGIN', user)
    },
    async logout({commit}){
      await reqLogout()
      commit('LOGOUT')
    }
  },
  mutations: {
    LOGIN(state, user) {
      localStorage.setItem('name',user.name),
      localStorage.setItem('token',user.token)

      state.name = user.name
      state.token = user.token
    },
    LOGOUT(state) {
      localStorage.removeItem("token");
      localStorage.removeItem("name");

      state.name = "";
      state.token = "";
    },
  },
}